﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLL;
using Model;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Reflection;
using System.Threading;

namespace NCRE学生考试端V1._0
{
    public class IEFindFiles
    {

        private readonly IeQuestionEntityBll iequestionbll = new IeQuestionEntityBll();

        #region"查找文件-韩梦甜修改-2015-11-20"
        /// <summary>
        /// 查找文件
        /// </summary>
        /// <param name="winquestion"></param>
        public void FindFile(IEQuestionEntity iequestion)
        {
            //将正确答案，分值取出来，传给studentRecord


            iequestion.questionFlag = "查找文件";

            DataTable ieQuestionDt = iequestionbll.LoadIEByFlag(iequestion);

            IEQuestionRecordEntity studentRecord = new IEQuestionRecordEntity();
            studentRecord.studentID = FrmLogin.studentID;
            studentRecord.timeStamp = MyInfo.TimeTamp();
            string correctAnswer;
            string fraction;
            string examAnswer;
            //循环遍历正确答案
            for (int i = 0; i < ieQuestionDt.Rows.Count; i++)
            {
                //将考生ID传到studentRecord实体
                studentRecord.studentID = FrmLogin.studentID;
                //将试题的ID取出来
                studentRecord.questionID = Convert.ToDouble(ieQuestionDt.Rows[i]["questionID"]);
                //将题的分数取出来
                fraction = ieQuestionDt.Rows[i]["fraction"].ToString();
                //将正确答案取出来
                correctAnswer = ieQuestionDt.Rows[i]["correctAnswer"].ToString();
                string paperType;
                paperType = MyInfo.MyPaperType();

                string str = @"D:\" + MyInfo.MystudentID() + @"\计算机一级考生文件\" + paperType + "卷" + @"\netkt" + correctAnswer;
                
                if (File.Exists(str))
                {

                    #region 判断存的txt文件内容是否正确————刁琳童————2018年12月22日

                    // 具体的txt文件名
                    string fileName = str.Split('\\')[str.Split('\\').Length - 1];

                    // 读取txt内容
                    StreamReader sr = new StreamReader(str, Encoding.Default);
                    string content = sr.ReadLine();

                    // 定义bool变量判断内容是否正确
                    bool resContent = false;
                    if (content != null)
                    {
                        switch (fileName)
                        {
                            // B卷
                            case "bjxx.txt":
                                resContent = content.Contains("不限行");
                                break;
                            // D卷
                            case "4001005780.txt":
                                resContent = content.Contains("是");
                                break;
                            // E卷
                            case "eID.txt":
                                resContent = content.Contains("免费");
                                break;
                            // F卷
                            case "PAAS.txt":
                                resContent = content.Contains("平台即服务");
                                break;
                            // G卷
                            case "SAAS.txt":
                                resContent = content.Contains("软件即服务");
                                break;
                            // H卷
                            case "IAAS.txt":
                                resContent = content.Contains("基础设施即服务");
                                break;
                        }
                    }
                   

                    if (resContent == true) // 如果txt内容正确，加满分
                    {
                        // 加满分
                        studentRecord.fraction = Convert.ToDouble(fraction);
                        examAnswer = correctAnswer;
                        studentRecord.examAnswer = examAnswer;
                    }
                    else //如果txt内容不正确，加一半的分
                    {
                        // 加一半分
                        studentRecord.fraction = Convert.ToDouble(fraction) / 2;
                        studentRecord.examAnswer = "";
                    }
                    #endregion

                }
                else
                {
                    //不加分
                    studentRecord.fraction = 0;
                    studentRecord.examAnswer = "";
                }
                IELoadInfo.list.Add(studentRecord);
            }
        }
        #endregion
    }

}
